package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.tools.REXTools;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class AddLinkmanActivity extends BaseActivity implements View.OnClickListener{
    private static final int FROM = 1;
    private ImageView linkback;
    private EditText addaddress;
    private EditText addhome;
    private EditText addname;
    private EditText addphone;
    private Button uplinkman;
    private String address;
    private String home;
    private String phone;
    private String name;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String is;
    private String type ;
    private CheckBox checkBox;
    private String name1;
    private String phone1;
    private String address1;
    private double latitude1;
    private double longitude1;
    public List<GeocodeAddress> geocodeAddressList;
    private double latitude;
    private double longitude;
    private String alatitude;
    private String alongitude;
    private String asnippet = null;
    private TextView title;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_linkman);
        Intent intent = getIntent();
        is = intent.getStringExtra("is");
        name1 = intent.getStringExtra("name1");
        phone1 = intent.getStringExtra("phone1");
        address1 = intent.getStringExtra("address1");
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");

        initAddView();

        if (is.equals("0")){
            title.setText("添加发件人");
        }else {
            title.setText("添加收件人");
        }
        // 设置默认
        type="0";
        addname.setText(name1);
        addphone.setText(phone1);
        addaddress.setText(address1);
    }
    private void initAddView() {
        backmain = (ImageView) findViewById(R.id.addlink_backmain);
        linkback = (ImageView) findViewById(R.id.addlink_back);
        addaddress = (EditText) findViewById(R.id.add_inaddress);
        addaddress.setKeyListener(null);
        addhome = (EditText) findViewById(R.id.add_inhome);
        addname = (EditText) findViewById(R.id.add_inname);
        addphone = (EditText) findViewById(R.id.add_inphone);
        uplinkman = (Button) findViewById(R.id.uplinkman);
        checkBox = (CheckBox) findViewById(R.id.add_chexck);
        title = (TextView) findViewById(R.id.addlink_typetitle);
        // 设置监听事件
        uplinkman.setOnClickListener(this);
        checkBox.setOnClickListener(this);
        linkback.setOnClickListener(this);
        addaddress.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addlink_backmain:
                startActivity(new Intent(AddLinkmanActivity.this,MainActivity.class));
                break;
            case R.id.addlink_back:
                finish();
                break;
            case R.id.add_inaddress:
                Intent intent = new Intent(AddLinkmanActivity.this,InPOISearchActivity.class);
                  startActivityForResult(intent,FROM);
                break;
            case R.id.uplinkman:// 提交数据
                getinData();

                if (!home.equals("")&!home.equals(null)){
                    if (asnippet!=null){
                        if (!phone.equals("")){
                            if (REXTools.isPhoneNumber(phone)){
                                if (!name.equals("")){
                                    sendLinkman();
                                }else {
                                    Toast.makeText(this, "请填写联系人姓名", Toast.LENGTH_SHORT).show();
                                }

                            }else {
                                Toast.makeText(this, "请填写正确的手机号码", Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(this, "请添加手机号", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this, "请添地址", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "请添加门牌号", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.add_chexck:// 选中状态

                if (checkBox.isChecked()){
                    type="1";
                }else {
                    type="0";
                }

                break;
        }
    }
    // 提交联系人数据
    private void sendLinkman() {
        getinData();
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/contact/addContact");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("name",name);
        object.put("phone",phone);
        object.put("address",asnippet);
        object.put("houseNumber",home);
        object.put("isDefault",type);
        object.put("type",is);
        object.put("longitude",alongitude);
        object.put("latitude",alatitude);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                finish();
                Toast.makeText(AddLinkmanActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                ErrType.errType(AddLinkmanActivity.this,ex);
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }
    //获取输入数据
    private void getinData() {
        address = String.valueOf(addaddress.getText());
        home = String.valueOf(addhome.getText());
        phone = String.valueOf(addphone.getText());
        name = String.valueOf(addname.getText());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==10){
            alatitude = data.getStringExtra("latitude");
            alongitude = data.getStringExtra("longitude");
            asnippet = data.getStringExtra("snippet");

            addaddress.setText(asnippet);
        }
    }


}
